#include <stdexcept>

#include "../include/Texture.h"

namespace GUL {
    namespace System {
        namespace Graphic {
            //    Texture::Texture() : m_Texture(NULL),m_TextureRenderer()
            //    {
            //        
            //    }

            Texture::Texture(Renderer& rend, SDL_Texture* tex, const std::string& name) : m_Texture(tex), m_TextureRenderer(rend),m_name(name) {
                if (tex == nullptr)
                    throw std::invalid_argument("Texture::Texture -> SDL_Texture have to be not a nullptr!!!\nThis Ctor is an CopyCtor\n");
                this->m_TextureRenderer.insertTexture(name, this);
            }

            Texture::Texture(Renderer& rend, UPair& Boundary, const std::string& name, Uint32 format, int access) : m_Texture(NULL), m_TextureRenderer(rend),m_name(name) {
                this->m_Texture = this->m_TextureRenderer.SDLCreateTexture(Boundary, format, access);
                if (this->m_Texture == nullptr)
                    throw std::runtime_error("Couldn't create a valid Texture! \n");
                this->m_TextureRenderer.insertTexture(name, this);
            }

            Texture::Texture(Renderer& rend, Surface& Surf, const std::string& name) : m_Texture(NULL), m_TextureRenderer(rend),m_name(name) {
                this->m_Texture = this->m_TextureRenderer.SDLCreateTexture(Surf);
                if (this->m_Texture == nullptr)
                    throw std::runtime_error("Couldn't create a valid Texture! \n");
                this->m_TextureRenderer.insertTexture(name, this);
            }

            Texture::Texture(Renderer& rend, SDL_Surface* Surf, const std::string& name) : m_Texture(NULL), m_TextureRenderer(rend),m_name(name) {
                this->m_Texture = this->m_TextureRenderer.SDLCreateTexture(Surf);
                if (this->m_Texture == nullptr)
                    throw std::runtime_error("Couldn't create a valid Texture! \n");
                this->m_TextureRenderer.insertTexture(name, this);
            }

            Texture::~Texture() {
                SDL_DestroyTexture(this->m_Texture);
            }
            
            std::string& Texture::name()
            {
                return this->m_name;
            }

            void Texture::QueryTexture(Uint32* format, UPair& Boundary) {
                
                int w =0, h=0;
                SDL_QueryTexture(this->m_Texture,format,NULL,&w,&h);
                Boundary.first = w;
                Boundary.second = h;

            }
                        
            void Texture::AlphaMod(Uint8 alpha) 
            {
                SDL_SetTextureAlphaMod(this->m_Texture, alpha);
            }
            
            void Texture::ColorMod(SDL_Color color) {
                
                SDL_SetTextureColorMod(this->m_Texture,color.r,color.g,color.b);
            }



        }
    }

}